<!DOCTYPE html>
<html>
<head>@include('/common/header.html',{"title":"控制台"}){}
<link rel="stylesheet" href="${baseStaticUrl}/plugins/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="${baseStaticUrl}/plugins/ztree/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="${baseStaticUrl}/plugins/ztree/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript">
    var zTreeObjMenu;
    var treeObj;
    var settingMenu = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: null
        }
    };
    var zNodesMenu = [{
        name: "菜单树",
        id: "0",
        open: false,
        children:${treeNodeJson}
    }];
    //页面加载后初始化zTree数据且默认展开所有节点  
    $(document).ready(function () {
        zTreeObjMenu = $.fn.zTree.init($("#sysMenuTree"), settingMenu, zNodesMenu).expandAll(true);
    });
    //角色树
    var zTreeObjRole;
    var settingRole = {
        check: {
            enable: false
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            beforeClick: zTreeOnClick
        }
    };
    var zNodesRole = [{
        name: "系统角色",
        id: "0",
        open: true,
        children:${jsonStr}
    }];
    $(document).ready(function () {
        zTreeObjRole = $.fn.zTree.init($("#roleTree"), settingRole, zNodesRole).expandAll(true);
    });
    var treeIdRole = null;
    //节点单击事件
    function zTreeOnClick(treeId, treeNode, clickFlag) {
        $.fn.zTree.init($("#sysMenuTree"), settingMenu, zNodesMenu).expandAll(true);
        treeIdRole = treeNode.id;
        $.ajax({
            url: "${basePath}/sys/rolemenu/getMenuIdByRoleId?treeIdRole=" + treeIdRole,
            data: "data",
            type: "post",
            error: function () {
                MessageBox.error("服务器错误，请稍后再试！");
            },
            success: function (data) {
                data = eval(data);
                if (data != null) {
                    for (var i = 0; i < data.length; i++) {
                        var treeObj = $.fn.zTree.getZTreeObj("sysMenuTree");
                        var node = treeObj.getNodeByParam("id", data[i].menu_id, null);
                        treeObj.checkNode(node, true, true, true);
                    }
                } else {
                    zTreeObjMenu = $.fn.zTree.init($("#sysMenuTree"), settingMenu, zNodesMenu).expandAll(true);
                }
            }
        });
    }
    function save() {
        treeObj = $.fn.zTree.getZTreeObj("sysMenuTree");
        var nodes = treeObj.getCheckedNodes(true);
        var treeNodes = [];
        for (var i = 1; i < nodes.length; i++) {
            if (isEmpty(nodes[i].children)) {
                if (i != nodes.length - 1) {
                    treeNodes.push(nodes[i].id);
                } else {
                    treeNodes.push(nodes[i].id);
                }
            }
        }
        var menuIds = JSON.stringify(treeNodes);
        if(isEmpty(treeIdRole)){
            MessageBox.error("请选择角色！");
            return;
        }
        if(isEmpty(menuIds)){
            MessageBox.error("请勾选菜单！");
            return;
        }
        $.post("${basePath}/sys/rolemenu/save?" + "menuIds=" + menuIds + "&treeIdRole=" + treeIdRole, null, function (result) {
            if(result.code != 0){
                MessageBox.error("保存失败！");
                return;
            }
            MessageBox.success("保存成功！");
        });
    }
</script>
</head>
<body>
<body class="hold-transition skin-blue sidebar-mini">
  	<div class='toolbar row'>
		<div class='col-xs-12 _common_head_bar_css'>
			<span class="headTitle">角色菜单分配</span>
			<div style="margin-top: 10px;margin-right: 10px;">
			<button id='${headToolbarId!"_common_head_bar"}_Save' style="display: none"
	                class="btn btn-primary btn-sm" >
	            <span class="glyphicon glyphicon-save" aria-hidden="true"></span>保存
	    	</button>
			</div>
		</div>
	</div>
	<section class="content">
		<div class="box box-primary">
			<div class="box-body">
				<div class="row">
					<div class="col-sm-2">
						 <ul id="roleTree" class="ztree"></ul>
					</div>
					<div class="col-sm-10">
						<div class="row visa-row marginZero" style="text-align: right">
		                    	<button id="delete_method" type="button" class="btn btn-danger btn-sm" onclick="save()">
									  <span class="glyphicon glyphicon-save" aria-hidden="true"></span>保存
								</button>
		                </div>
		                <div class="row visa-row">
		                    <ul id="sysMenuTree" class="ztree"></ul>
		                </div>
					</div>
				</div>
			</div>
		</div>
	</section>
</body>
</html>